//  Created by 莫瑞权 on 2022/6/14.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_1vN_Live: TF_1vN_LiveMain,TF1to16VideoAreaDelegate,UITextFieldDelegate,TFWhiteboardToolsDelegate,TFSetButtonViewDelegate,TFGalleryViewDelegate,TFkeyNoteViewDelegate,TFFullViewDelegate,TalkfunSDKLiveDelegate,TalkfunSDKRtcDelegate,TFMarqueeDelegate {
    
    var chatDisable:Bool = false
    var chatDisableAll:Bool = false
    var isRefresh:Bool = false  //刷新sdk,防止画板被覆盖
    var zhuboName:String = ""  // 主播名字
    let pageModeView = UIView()// 切换视频模式的父类
    var zhujiangStatus:NSInteger = 0 //主讲状态
    var zhujiangxid:String = "" //主讲人xid
    let timerView = TFTimerView() //定时倒计时
    let responderView = TFResponderView() //抢答
    let turntableView = TFTurntableView() //转盘
    let below = TalkfunHitTestView()
    let listOfWinners = TFListOfWinners() //获奖名单
    let desktopContainerView = TFDesktopContainerView()//桌面分享容器
    let mediaContainerView = TFMediaContainerView()
    var isDoubleTouch:Bool = false //双击放大
 
    override func viewDidLoad() {
        super.viewDidLoad()
        //小班模式
        self.modetype = "5"
        //0：非小班
        //1：双人，
        //2：1V6 模式，
        //3：1V16模式
        //先配置用于初始化ui,后期可读值elf.talkfunSDK.roomInfo.usercamera.smallType 更改 self.smallType
        self.smallType = "3"
        
        self.fullView.delegate = self
        self.pageModeView.backgroundColor = self.superView.backgroundColor
        self.pageModeView.frame = self.superView.bounds
        self.superView.addSubview(self.pageModeView)
       
        //TODO: 2.桌面分享
        self.addDesktopView()
        //TODO:3.音频视频容器
        self.addMediaView()
        //TODO:初始化小班SDK
        
        let talkfunSDK = self.inittalkfunSDK(self.access_token, courseType: TalkfunCourseTypeStatusRTC)
        talkfunSDK.delegate = self
        //小班的代理
        talkfunSDK.rtcManager.delegate = self
        
        // 配置桌面分享容器
        self.talkfunSDK.configureDesktopContainerView(self.desktopContainerView)
        // 配置桌面音视频容器
        self.talkfunSDK.configureMediaContainerView(self.mediaContainerView.playVIew)
        self.isRefresh = true
        //TODO: 3 添加互动工具类
        self.addInteractToos()
       
       //TODO: 8.0网络错误
        self.addTallkFenPromptTool()
        self.superView.addSubview(self.promptTool)
       //TODO: 1.添加导航栏
        self.navigationBarTools1to16()
        //讲台的相关配置
        self.addPodiumConfiguration()
        self.superView.addSubview(self.keyboardTool)
    
      //TODO: 聊天列表
        self.addChatList()
       //TODO: 投票列表
        self.addVotingArea()
       //TODO: 回答
        self.qskQuestions()
       //TODO: 键盘
        self.addKeyboardArea()
    }
   func addMediaView()
    {
        self.superView.addSubview(self.mediaContainerView)
        let  mediaAreaY:CGFloat = self.videoH()
        self.mediaContainerView.frame = CGRect(x: 0, y: mediaAreaY, width: self.superView.frame.size.width, height: self.superView.frame.size.height-mediaAreaY)
         
    }
    func addDesktopView()
    {
        //TODO:桌面分享容器
        self.desktopContainerView.isUserInteractionEnabled = false
        self.desktopContainerView.backgroundColor = UIColor.clear
        self.desktopContainerView.isHidden = true
          self.superView.addSubview(self.desktopContainerView)
        
        self.desktopContainerView.frame = CGRect(x: 0, y: self.videoH(), width: self.superView.frame.size.width, height: self.superView.frame.size.height-self.videoH())
    }
     @objc func timerViewClick()
    {
    }
    @objc func navigationBarTools1to16()
     {

         self.superView.addSubview(self.navigationBar)
         self.navigationBar.frame = self.superView.bounds
         self.navigationBar.addTopViewAndBottomView()
         self.navigationBar.setButton.isHidden = true
         self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
       
         self.navigationBar.backgroundColor = UIColor.clear

         //TODO:返回
         self.navigationBar.backButtonAddTarget(self, action: #selector(btnClick))
       
         //TODO:设置
         self.navigationBar.setButtonTools.addTarget(self, action: #selector(setButtonToolsClick), for: .touchUpInside)
      
         //TODO:全屏
         self.navigationBar.isFullButton.addTarget(self, action: #selector(isFullButtonClicck), for: .touchUpInside)
        
         self.navigationBar.title.text = ""
         self.navigationBar.time.isHidden = true

         //TODO:刷新
         self.navigationBar.refresh.addTarget(self, action: #selector(refresh), for: .touchUpInside)
          //TODO: iphone 聊天列表显示隐藏 事件
         self.navigationBar.chat.addTarget(self, action: #selector(cahtButtonClick), for: .touchUpInside)
         //TODO:启动聊天输入栏
         self.navigationBar.inputFieldButton.addTarget(self, action: #selector(inputFieldButtonClick), for: .touchUpInside)
         //TODO:申请上讲台
         self.navigationBar.ondium.addTarget(self, action: #selector(PodiumClick), for: .touchUpInside)
         //TODO:提问列表
         self.navigationBar.askQuestions.addTarget(self, action: #selector(askQuestionsClick), for: .touchUpInside)
         //TODO:投票列表
         self.navigationBar.voteBtn.addTarget(self, action: #selector(questionButtonClick), for: .touchUpInside)
         //TODO:课件下载
         self.navigationBar.courseDownBtn.addTarget(self, action: #selector(courseDownBtnClick), for: .touchUpInside)
         
     }

    @objc func isFullButtonClicck(_ btn:UIButton)
    {
        self.courseLive.drawing.doubleTapActionBlock!()
    }
    //刷新
    @objc func refresh()
    {
        if(self.talkfunSDK.isRefresh==false){
            self.navigationBar.switchButton.setImage(UIImage.init(named: "HTLive.bundle/bigPpt"), for: .normal)
            //先隐藏音视频
            self.mediaContainerView.playStatus(TalkfunMultiMediaStatusChangeListenerClose, time: 0)
          
            self.promptTool.loading.isHidden = false
            
             if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
             
                 self.courseLive.videoArea.videoSessionsArray.removeAllObjects()
                 //刷新之后,画板返回原来的状态
                 if (self.isDoubleTouch) {
                     let drawingY:CGFloat = self.courseLive.videoArea.frame.maxY
                     self.courseLive.drawing.frame = CGRect(x: 0, y: self.courseLive.videoArea.frame.maxY, width: self.courseLive.view.frame.size.width, height: self.superView.frame.size.height-drawingY)
                    
                 }
             } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {  self.keynoteLive.videoArea.videoSessionsArray.removeAllObjects()
                 self.keynoteLive.keynoteView.videoArr.removeAllObjects()
             } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
                 self.galleryLive.videoArea.videoSessionsArray.removeAllObjects()
             }
          
            self.isRefresh = false
            
            self.talkfunSDK.reload()
          
        }
      
    }

    @objc func cahtButtonClick()
    {
        self.isShowTopViewAndBottomView(false)
        if (self.drawPower && self.talkfunSDK.liveStatus == TalkfunLiveStatusStart) {
            self.courseLive.drawing.whiteboardTools.isHidden  = false
        } else {
            self.courseLive.drawing.whiteboardTools.isHidden  = true
        }

        self.isShowVotingArea(false)
        self.showChatArea(true)
        self.navigationBar.chat.clearRedDot()
    }

    func lineButtonClick()
    {
        self.talkfunSDK.rtcManager.whiteboard.drawType = self.drawType
    }
    func eraserButtonClick()
    {
        self.courseLive.drawing.whiteboardTools.isHidden = true
        self.drawType = self.talkfunSDK.rtcManager.whiteboard.drawType
        self.talkfunSDK.rtcManager.whiteboard.drawType =  TalkfunDrawTypeBoxDelete
        
    }
    //讲台的相关配置
    func addPodiumConfiguration()
    {
            //是1v6了, 隐藏提问
        if (self.smallType == "2") {
            self.courseLive.drawing.podiumConfiguration.askQuestions.isHidden = true
            self.navigationBar.askQuestions.isHidden = true
        }
        
    }
    @objc func PodiumClick(_ btn:UIButton)
    {
        
        //未申请
        if (self.talkfunSDK.rtcManager.applyStatus == .noApply) {
            
            
            let  alerts = self.showContent("是否申请连麦?", Message: "", Cancel: "取消", Sure: "确定")
            
            alerts.resultIndex = { [weak self] () in
                
                guard let `self` = self else { return }
                //申请上讲台
                self.talkfunSDK.rtcManager.apply {[weak self] code, error in
                    
                    guard let `self` = self else { return }
                    
                        if (code == 0) {
                            self.TF_toast("你已申请连麦")
           

                            self.navigationBar.ondium.onPodium(.applying)
            
                            self.courseLive.drawing.podiumConfiguration.podiumStatus(.applying)
                     
                            
                        }else {
                            
                            self.TF_toast(error!.msg)
                            
                        }
                        
              
                    alerts.removeFromSuperview()
              
                }
            }
           
           
         //已经申请上讲台
        }else if (self.talkfunSDK.rtcManager.applyStatus == .applying) {
         
            
            let  alerts = self.showContent("是否取消申请连麦?", Message: "", Cancel: "取消", Sure: "确定")
            
            alerts.resultIndex = { [weak self] () in
                
                guard let `self` = self else { return }

                alerts.removeFromSuperview()

                    //老师已经让自己上讲台
                if ( self.talkfunSDK.rtcManager.applyStatus == .allow) {
                        return
                    }
                
                self.talkfunSDK.rtcManager.cancel {[weak self] code, error in
                    
                    guard let `self` = self else { return }
          
                        if (code == 0) {
                            self.TF_toast("你已取消申请连麦")
                            self.navigationBar.ondium.onPodium(.noApply)
                            self.courseLive.drawing.podiumConfiguration.podiumStatus(.noApply)
                
                           
                          
                        }
                        
                      
                }
                
                
            }
            
        //讲台上
        }else if (self.talkfunSDK.rtcManager.applyStatus == .allow) {
            let  alerts = self.showContent("是否取消连麦?", Message: "", Cancel: "取消", Sure: "确定")
            
            //点击事件
            alerts.resultIndex = { [weak self] () in
                
                guard let `self` = self else { return }
                alerts.removeFromSuperview()
          
                //申请下讲台
                self.talkfunSDK.rtcManager.downRTC {[weak self] code, error in
                    
                    guard let `self` = self else { return }
                    
           

                        if (code == 0) {
                  
                               //1.不是1v6  2.已经是连麦了
                            if(self.smallType != "2" && self.talkfunSDK.rtcManager.allowApply == false){
                                 
                                   self.courseLive.drawing.podiumConfiguration.onPodium.isHidden = true
                                 
                                }
                            if (self.modetype == "6"){
                              self.isShowvCameraView(true)
                                self.courseLive.videoArea.videoSessionsArray.removeAllObjects()
                            }
                            self.TF_toast("你已取消连麦")
                                
                                if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
                                    self.courseLive.videoArea.removeFromVideo(self.talkfunSDK.rtcManager.getUserModel())
                                    self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
                                   
                                } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
                                    self.keynoteLive.videoArea.removeFromVideo(self.talkfunSDK.rtcManager.getUserModel())
                                    
                                    self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
                                   
                                } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
                                    self.galleryLive.videoArea.removeFromVideo(self.talkfunSDK.rtcManager.getUserModel())
                                  
                                    
                                    self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "2")
                                }
                            
                            self.navigationBar.ondium.onPodium(.noApply)

                                self.courseLive.drawing.whiteboardTools.isHidden = true
                    
                            
                        }else {
                            
                            self.TF_toast(error!.msg)
                            
                        }

                }
                
                
            }

        }
       
    }

    func setMicroPhoneDeviceStatusUI(_ open:Bool)
    {
        
        if (open) {
            self.talkfunSDK.rtcManager.setMicroPhoneDeviceStatus(true) {[weak self] code, error, model in
                guard let `self` = self else { return }
                if(code==0)
                {
                    self.TF_toast("你的麦克风已开启")
                    if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
                        self.courseLive.videoArea.updateModel(model)
                    } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
                        self.keynoteLive.videoArea.updateModel(model)
                    } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
                        self.galleryLive.videoArea.updateModel(model)
                    }
                 
                }else {
            
                    self.TF_toast(error.msg)
                }
            }
        }else
        {
            self.talkfunSDK.rtcManager.setMicroPhoneDeviceStatus(false) {[weak self] code, error, model in
                guard let `self` = self else { return }
                
                if(code==0)
                {
                    self.TF_toast("你的麦克风已关闭")
                    if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
                        self.courseLive.videoArea.updateModel(model)
                    } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
                        self.keynoteLive.videoArea.updateModel(model)
                    } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
                        self.galleryLive.videoArea.updateModel(model)
                    }
                }else {
                    self.TF_toast(error.msg)
                }

            }
        }

    }

    //#pragma mark  添加互动工具
    func addInteractToos()
    {
        //TODO:定时器(倒计时)
         self.timerView.isHidden = true
         self.superView.addSubview(self.timerView)
         self.timerView.close.addTarget(self, action: #selector(timerViewClick), for: .touchUpInside)
         
        //TODO:抢答
         self.responderView.isHidden = true
         self.responderView.backgroundColor = UIColor(red: 33/255.0, green: 38/255.0, blue: 46/255.0, alpha: 0.9)
         self.responderView.layer.cornerRadius = 10
         self.responderView.layer.masksToBounds = true
         self.superView.addSubview(self.responderView)
         self.responderView.responderButton.addTarget(self, action: #selector(responderButtonClick), for: .touchUpInside)
       
        //TODO:转盘
       self.turntableView.isHidden = true
       self.superView.addSubview(self.turntableView)
        
        //TODO:跑马灯
        self.marquee.delegate = self
        self.superView.addSubview(self.marquee)
        
        //TODO:抽奖
        self.superView.addSubview(self.below)
        
//        self.lottery.isUserInteractionEnabled = true
//        self.lottery.image = UIImage.init(named: "HTLive.bundle/lottery1vN")
//        self.lottery.isHidden = true
//        self.below.addSubview(self.lottery)
        
        //TODO:中奖名单
        self.listOfWinners.isHidden = true
        self.listOfWinners.backgroundColor = UIColor(red: 19/255.0, green: 21/255.0, blue: 25/255.0, alpha: 0.9)
        self.below.addSubview(self.listOfWinners)
        self.listOfWinners.layer.cornerRadius = 10
        self.timerView.frame = CGRect(x: (self.superView.frame.size.width - 120)/2, y: (self.superView.frame.size.height - 120)/2, width: 120, height: 120)
  
        
        self.responderView.frame = CGRect(x: (self.superView.frame.size.width - 200)/2, y: (self.superView.frame.size.height - 200)/2, width: 200, height: 200)
        
            let turntableViewW:CGFloat = 155-12.5
            
        self.turntableView.frame =  CGRect(x: (self.superView.frame.size.width - turntableViewW)/2, y: (self.superView.frame.size.height - turntableViewW)/2, width: turntableViewW, height: turntableViewW)
        self.below.frame = self.superView.bounds
        
//        self.lottery.frame = CGRect(x: (self.below.frame.size.width - 235)/2, y: (self.below.frame.size.height - 111)/2, width: 235, height: 111)
            
         
        
        self.listOfWinners.frame = CGRect(x: (self.below.frame.size.width-250)/2, y: (self.below.frame.size.height-210)/2, width: 250, height: 210)
 
//        let statusHeight = UIApplication.shared.statusBarFrame.height
        self.marquee.frame = CGRect(x: 0, y: 40, width: self.superView.frame.size.width, height: 32)
       
      if (self.marquee.frame.size.width != self.marquee.view.frame.size.width) {
          self.marquee.refreshData()
      }
        
    }
                                  
    func updatekeyNoteNameFrame(_ name:String)
    {
            
        let rect = TalkfunSharedTools.getRectWith(name, size: CGSize(width: 200, height: 20), fontSize: 11)
        self.keynoteLive.keynoteView.nameBackgroudView.frame = CGRect(x: 5, y: self.keynoteLive.keynoteView.view.frame.size.height-20, width: rect.size.width+10, height: 20)
            
          
        self.keynoteLive.keynoteView.nameLab.frame = CGRect(x: 5, y: 0, width: rect.size.width, height: 20)
            
    }
    
    //更新keyNoteframe
   func updetaKeyfalseteViewFrame(_ isOpen:Bool)
    {
        var drawingY:CGFloat = 0

        if (isOpen) {
            drawingY = self.keynoteLive.videoArea.frame.maxY
        } else {
            drawingY = 0
        }

        if (self.modetype == "6")
        {
            drawingY = 0
        }
       let drawingH = self.keynoteLive.view.frame.size.height-drawingY

        let  drawingW = drawingH/3.0*4;

        let drawingX = (self.keynoteLive.view.frame.size.width - drawingW)/2.0
        self.keynoteLive.keynoteView.frame = CGRect(x: drawingX, y: drawingY, width: drawingW, height: drawingH)
        
    }

    func touchesBeganUI()
    {
        self.isShowOtherArea(false)
        self.isShowSetButtonToolsArea(false)
        if (self.navigationBar.topView.frame.origin.y == 0) {
            self.isShowTopViewAndBottomView(false)
          
        }else{
             self.isShowTopViewAndBottomView(true)
            
        }

       self.showChatArea(false)
        self.isShowVotingArea(false)
        self.isShowqskQuestionsView(false)
    }
  //  #pragma mark  画廊模式
    func addgalleryMode()
    {
   
        self.galleryLive.view.frame = self.pageModeView.bounds
        self.galleryLive.view.backgroundColor = self.pageModeView.backgroundColor
        self.pageModeView.addSubview(self.galleryLive.view)
        
        self.galleryLive.addTouchView()
        self.galleryLive.videoArea.delegate = self
        if (self.talkfunSDK.roomInfo.mod_zhuboaction_live.config.cup.enable) {
            self.galleryLive.videoArea.showCup = true
        } else {
            self.galleryLive.videoArea.showCup = false
        }
        
        //画廊模式
        self.galleryLive.videoArea.collectionView!.touchesBegan =  { [weak self] () in
            
            guard let `self` = self else { return }
            self.touchesBeganUI()
        }

    }

   // #pragma mark  主讲模式
    func addkeynoteMode()
    {

        self.keynoteLive.videoArea.pageMode = self.talkfunSDK.pageMode;
        self.keynoteLive.view.frame = self.pageModeView.bounds;
        self.keynoteLive.view.backgroundColor = self.pageModeView.backgroundColor;
        self.pageModeView.addSubview(self.keynoteLive.view)
        self.keynoteLive.addvideoArea()
        
        self.keynoteLive.videoArea.delegate = self
        self.keynoteLive.keynoteView.delegate = self

        self.updetaKeyfalseteViewFrame(false)
        
        if (self.talkfunSDK.roomInfo.mod_zhuboaction_live.config.cup.enable) {
            self.keynoteLive.videoArea.showCup = true
        } else {
            self.keynoteLive.videoArea.showCup = false
        }
   
        //主讲模式
        self.keynoteLive.keynoteView.touchesBegan = { [weak self] () in
            
            guard let `self` = self else { return }
            self.touchesBeganUI()
        }
        
    }
    //#pragma mark  课程模式
    func addCourseMode()
    {

        self.courseLive.videoArea.pageMode = self.talkfunSDK.pageMode;
        self.courseLive.view.frame = self.pageModeView.bounds;
        self.courseLive.view.isHidden = false
        self.courseLive.view.backgroundColor = self.pageModeView.backgroundColor;
        self.pageModeView.addSubview(self.courseLive.view)
        self.courseLive.addvideoArea()
        
        self.courseLive.drawing.podiumConfiguration.askQuestions.isHidden = true
        self.courseLive.drawing.podiumConfiguration.vote.isHidden = true
        self.courseLive.drawing.whiteboardTools.delegate = self
        self.courseLive.videoArea.delegate = self;
        let drawingX:CGFloat = 0
        var drawingY = self.courseLive.videoArea.frame.maxY
        let  drawingW = self.superView.frame.size.width
          if (self.modetype == "6")
          {
              drawingY = 0
          }
         let drawingH = self.superView.frame.size.height-drawingY
          self.courseLive.drawing.frame = CGRect(x: drawingX, y: drawingY, width: drawingW, height: drawingH)
        
     
          self.courseLive.drawing.tempFrame = self.courseLive.drawing.frame
        
        if (self.talkfunSDK.roomInfo.mod_zhuboaction_live.config.cup.enable) {
            self.courseLive.videoArea.showCup = true
        } else {
            self.courseLive.videoArea.showCup = false
        }

            
            self.courseLive.drawing.doubleTapActionBlock = { [weak self] () in
                
                guard let `self` = self else { return }
        
                if (self.talkfunSDK.rtcManager.applyStatus != .allow) {
        
                    if (self.courseLive.drawing.frame.size.height == self.courseLive.view.frame.size.height && self.modetype == "6") {
                        return;
                    }
                }
    
                UIView.animate(withDuration: 0.5) {
                             self.isShowqskQuestionsView(false)
                            self.showChatArea(false)
                           self.isShowVotingArea(false)
                        if ( self.courseLive.drawing.frame.size.height != self.courseLive.view.frame.size.height) {
                            self.navigationBar.isFullButton.setImage(UIImage.init(named: "HTLive.bundle/shrink1vN"), for: .normal)
                       
                            self.courseLive.view.frame = self.superView.bounds
                            self.courseLive.drawing.frame = self.courseLive.view.bounds
                            
                            self.desktopContainerView.frame = self.courseLive.drawing.frame
                            
                            self.courseLive.drawing.isFullButton.setImage(UIImage.init(named: "HTLive.bundle/ppt_narrow"), for: .normal)
                           
                            self.courseLive.videoArea.isHidden = true
                            self.isDoubleTouch = true
        
                        }else{
                            self.courseLive.drawing.isFullButton.setImage(UIImage.init(named: "HTLive.bundle/ppt_enlarge"), for: .normal)
                           
                            self.navigationBar.isFullButton.setImage(UIImage.init(named: "HTLive.bundle/fullScreen1vN"), for: .normal)
                            
                             self.courseLive.drawing.frame =   self.courseLive.drawing.tempFrame
                     
                            self.courseLive.videoArea.isHidden = false
                            self.desktopContainerView.frame = self.courseLive.drawing.frame
                            self.isDoubleTouch = false

                        }
        
                }
        
        
        
            }
        //课件模式
            self.courseLive.drawing.whiteboardDidTouchedBlock =  { [weak self] () in
                
                guard let `self` = self else { return }
                self.touchesBeganUI()
          
            };
        
        self.talkfunSDK.configurePPTContainerView(self.courseLive.drawing.view)

    }
   // #pragma 代理画板当前页的frame
    func whiteboardPageFrame(_ frame: CGRect)
    {
        self.courseLive.drawing.isFullButton.frame = CGRect(x: frame.origin.x + frame.size.width-5-33, y: frame.origin.y + frame.size.height-5-33, width: 33, height: 33)
        
        
        let height = self.mediaContainerView.frame.size.height

            self.mediaContainerView.pageFrame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: height)

        if (self.isDoubleTouch) {
            self.updatePlayVIewFrame(true)
        } else {
            self.updatePlayVIewFrame(false)
        }
        
    }
        
        
    func updatePlayVIewFrame(_ isfull:Bool)
    {
        if (self.mediaContainerView.pageFrame.size.width>0&&self.mediaContainerView.docType != 0 ) {
               //音频
             if (self.mediaContainerView.docType == 5) {
                 self.mediaContainerView.playVIew.frame =  self.mediaContainerView.pageFrame

             }else if (self.mediaContainerView.docType == 4 ) {
              //视频
                 var  deskAreaY:CGFloat = 0.0
                 if (isfull) {
                     deskAreaY = 0
                 } else {
                     deskAreaY = self.videoH()
                 }
        
                 let height = self.superView.bounds.size.height - deskAreaY
                 
                 var playVIewWidth = height/(9.0/16.0)
                 
                 if (playVIewWidth>self.superView.bounds.size.width) {
                     playVIewWidth = self.superView.bounds.size.width
                 }
                 self.mediaContainerView.frame = CGRect(x: 0, y: deskAreaY, width: self.superView.frame.size.width, height: self.superView.frame.size.height-deskAreaY)

                 self.mediaContainerView.playVIew.frame = CGRect(x: (self.superView.frame.size.width - playVIewWidth)/2, y: 0, width: playVIewWidth, height: height)
                 
             }

        }
     
    }
 //   #pragma mark 退出
    @objc func btnClick()
    {
        let alert = self.showContent("你确定要退出直播间吗?", Message: "", Cancel: "取消", Sure: "确定")

        alert.resultIndex = { [weak self] () in

            guard let `self` = self else { return }

            if (self.talkfunSDK.rtcManager.applyStatus == .allow) {

                self.talkfunSDK.leaveliveroom(self.access_token) { [weak self]  (code, error) in
                    guard let `self` = self else { return }
                    if (code == 0) {

                        self.exit()

                    }

                }

              } else {
                  self.exit()

          }
            alert.removeFromSuperview()

          }

      }

      func exit()
    {
        self.UIInterfaceOrientationPortrait()
        
        self.mediaContainerView.removeObserver()
        
    
        self.marquee.destroy()
      
        NotificationCenter.default.removeObserver(self)
        self.qa_sendTimer?.invalidate()
        self.qa_sendTimer = nil
        self.chat_sendTimer?.invalidate()
        self.chat_sendTimer = nil
        self.awardView.animationDidStop()

        self.promptTool.destroy()
        self.talkfunSDK.destroy()
        self.navigationBar.destroy()
     
        //返回上一页
        self.TF_dismissUI()

    }
    /**
     设置开关 tag
     0.摄像头,
     1.前置,
     2.镜像,
     3.美颜,
     4.麦克风,
     5.扬声器,
     6.弹幕

     */
   // #pragma mark 设置用户开关
    func SetUserFunction(_ tag: NSInteger, andSwitch: UISwitch) {
        
        let isopen = andSwitch.isOn

        if (tag == 0)
        {
            //摄像头开关
            if(self.videoFrequentlyClick == true){
                self.TF_toast("点击太快啦，稍等一下再试试吧")
           
                return
            }
            self.videoFrequentlyClick = true;

            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1.0) {
                self.videoFrequentlyClick = false
            }
           

            let closeVideoState = TalkfunSettingToolsManager.shared().getVideoStatus(self.talkfunSDK.xid)
            
            
             if (closeVideoState) {
                 
                 self.TF_toast("主播关闭了你的摄像头")
                 return;
             }

            if (self.talkfunSDK.rtcManager.applyStatus == .allow) {
                self.talkfunSDK.closeVideoBeforeOnPodium = false
                
                
                self.talkfunSDK.rtcManager.setCameraDeviceStatus(!isopen) {[weak self] (code , error, model) in
                    guard let `self` = self else { return }
            
                        if (code==0) {
                            if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
                                self.courseLive.video1to1Arae.updateModel(model)
                            } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
                                self.keynoteLive.videoArea.updateModel(model)
                            } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
                                self.galleryLive.videoArea.updateModel(model)
                            }

                        }else
                        {
                            self.TF_toast(error.msg)
                        }

                }

            }
             else {
                self.talkfunSDK.closeVideoBeforeOnPodium = true
            }
            
            andSwitch.isOn = !isopen
            TalkfunSettingToolsManager.shared().setToolsSwitchVideo(!isopen, xid: self.talkfunSDK.xid)
        }
    
        else  if (tag == 1) {
            //前置/后置
            andSwitch.isOn = !isopen
            self.setButtonView.upDataLanguage()

            self.talkfunSDK.rtcManager.switchCamera()

            TalkfunSettingToolsManager.shared().setToolsSwitchCamera(!isopen, xid: self.talkfunSDK.xid)


        } else if (tag == 2) {
            //镜像开关
            self.talkfunSDK.rtcManager.setLocalViewMirror(isopen)
            TalkfunSettingToolsManager.shared().setToolsSwitchMirror(!isopen, xid: self.talkfunSDK.xid)

        } else if (tag == 3) {
            //美颜开关
            self.talkfunSDK.rtcManager.setBeautyEffectOptions(isopen)

            TalkfunSettingToolsManager.shared().setToolsSwitchBeauty(isopen, xid: self.talkfunSDK.xid)


        } else if (tag == 4) {
         //麦克风开关
            if(self.audioFrequentlyClick == true){
                self.TF_toast("点击太快啦，稍等一下再试试吧")
                return
            }
            self.audioFrequentlyClick = true

            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1) {
                self.audioFrequentlyClick = false
            }


            let closeAudioState = TalkfunSettingToolsManager.shared().getAudioStatus(self.talkfunSDK.xid)

             if (closeAudioState) {
                 self.TF_toast("主播关闭了你的麦克风")

                 return;
             }


            if (self.talkfunSDK.rtcManager.applyStatus == .allow) {
                self.talkfunSDK.closeAudioBeforeOnPodium = false

                self.setMicroPhoneDeviceStatusUI(!isopen)
              
            } else {
                self.talkfunSDK.closeAudioBeforeOnPodium = true
            }
            
            TalkfunSettingToolsManager.shared().setToolsSwitchAudio(!isopen, xid: self.talkfunSDK.xid)

            andSwitch.isOn = !isopen
        } else if (tag == 5) {
            //扬声器开关
            self.talkfunSDK.rtcManager.muteAllRemoteAudio(!isopen)
            TalkfunSettingToolsManager.shared().setToolsSwitchSpeaker(isopen, xid: self.talkfunSDK.xid)

        } else if (tag == 6) {
            //弹幕开关
            andSwitch.isOn = !isopen
   
            TalkfunSettingToolsManager.shared().setToolsSwitchBarrage(!isopen, xid: self.talkfunSDK.xid)
    
            if andSwitch.isOn  {
                
            }else
            {
                self.barrageRender.bulletSwitch = false

            }
            self.barrageRender.bulletSwitch = andSwitch.isOn
          
        }else if (tag == 7) {
            //中英文切换
            if (andSwitch.isOn == false) {
                  //调用 系统设置配置语言
                TFChangeLanguage.sharedInstance.setLanguage(language: "en")

            } else {
                TFChangeLanguage.sharedInstance.setLanguage(language: "zh-Hans")

            }
            andSwitch.isOn = !andSwitch.isOn

            //sdk 保存
            TalkfunSettingToolsManager.shared().setToolsLanguageChange(andSwitch.isOn, xid: self.talkfunSDK.xid)

            self.updataLanguage(isopen)

        }
    }

    //#pragma mark 中英文切换
    func updataLanguage(_ isyes:Bool)
    {
        self.setButtonView.upDataLanguage()
        self.navigationBar.upDataLanguage()
        self.keyboardTool.layoutSubviews()
        
        //更换语言
        self.qskQuestionsView.upDataLanguage()
        self.chatArea.upDataLanguage()
        self.signInView.upaDataLanguage()
        self.timerView.upDataLanguage()
        self.responderView.upDataLanguage()
        self.navigationBar.ondium.upDataLanguage(isyes)
        self.votingArea.voteDetail.upDataLanguage()
        self.votingArea.upDataLanguage()
        self.listOfWinners.updateLanguage()
        self.chatArea.tableView.reloadData()
    }

   // #pragma mark  音视频代理
    /**
     * @param status 播放状态
     * @param time   播放时间
     */
    func multiMediaApplicate(_ status: TalkfunMultiMediaStatusChangeListener, time: Int32) {

        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
        
            self.mediaContainerView.playStatus(status , time:NSInteger(time))
       
            ////关闭
            if (status == TalkfunMultiMediaStatusChangeListenerClose) {
                //是否应用了
                self.mediaContainerView.isMediaApplicatea = false
                if (self.navigationBar.topView.frame.origin.y == 0 && self.talkfunSDK.rtcManager.whiteboard.graffiti) {
                    self.courseLive.drawing.whiteboardTools.isHidden = false
                }
            }else if (status != TalkfunMultiMediaStatusChangeListenerError) {
                //是否应用了
                self.mediaContainerView.isMediaApplicatea = true
            }
        }
    }

    /* 应用
     *@param id 素材id
     * @param docType  5：音频 ，4：视频
     * @param title 素材标题
     * @param duration 视频时长
     */
    func multiMediaApplicate(_ ID: String, docType type: Int32, title: String, duration: Int32) {
    
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            if (type==0) {
                self.courseLive.drawing.isFullButton.isHidden = true
            }
//            NSLog(@"视频时长视频时长视频时长");
            self.mediaContainerView.docType( Int(type), duration:Int(duration))
           
            if (self.mediaContainerView.docType == 4) {
                self.courseLive.drawing.whiteboardTools.isHidden = true
            }
            self.mediaContainerView.isMediaApplicatea = true
            self.updatePlayVIewFrame(false)
        }
        

    }

  //  #pragma mark  定时器倒计时的代理
    /* 定时器开始倒计时
    *@param total 总时长
    * @param duration 剩余的时长倒计
    */
    func timerStartWidthTotal(_ total: Int, duration: Int) {

        self.timerView.isHidden = false
        self.timerView.timerStartWidthTotal(total, duration:duration)
    }

    /*
    // 定时器暂停
    // @param duration 剩余的时长倒计
    */
    func timerPause(_ duration: Int) {
 
        self.timerView.timerPause(duration)
    }
    /* 结束定时器
     */
    func timerClose() {
   
        self.timerView.isHidden = true
    }
    /*
     定时器重新开始
    */
    func timerReReady() {
  
        self.timerView.endTimeLaer()
    }
    /*
      定时器倒计时完成
    */
    func timerEnd() {
   
        self.timerView.endTimeLaer()
    }
  //  #pragma mark 开始抢答
  @objc func responderButtonClick()

    {
            self.talkfunSDK.sendResponder {[weak self] (code, error) in
                guard let `self` = self else { return }
                if(code==0)
                {
                    self.responderView.isHidden = true
                }
            }

    }
    //#pragma mark   抢答器的代理

    func responderApplicate(_ dict: [AnyHashable : Any]) {
   
        self.responderView.isHidden = false
        self.responderView.backgroundView1.isHidden = false
        self.responderView.backgroundView2.isHidden = true
    }
    /*
       抢答器开始共用了多少秒
    */
    
    func responderStartWidthDuration(_ duration: Int) {
        
        self.responderView.responderStartWidthDuration(duration)
    }
    /*
       抢答器关闭
    */
    func responderClose() {

        self.responderView.isHidden = true
    }
    /*
      定时器归0
    * @param duration 剩余的时长倒计
    */
    func responderReReady() {
   
        self.responderView.responderStartWidthDuration(0)
    }
    /* 抢答器结束
    */
    func responderEnd() {
 
        self.responderView.isHidden = false
        self.responderView.backgroundView1.isHidden = false
        self.responderView.backgroundView2.isHidden = true
        self.responderView.timeLabel.text = "00:00";
        self.responderView.noOne()
    }
    /* 抢答器成功的学员名字
     */
    
    func responderSecond(_ name: String, second duration: Int, isMe: Bool) {

        if (isMe==false) {
            //失败
            self.responderView.fail(name , xid:self.talkfunSDK.xid)
        }else{

            self.responderView.setResponder(name , duration:duration, xid:self.talkfunSDK.xid)
        }
       
    }
 //   #pragma mark  转盘的代理
    //转盘应用
    func turntableApplicate() {
   
        self.turntableView.turntableApplicate()
    }
    /* 转盘应用
       selected 选中的号码
       frequency 转盘未关闭前共转了多少次
    */
    
    func turntableSelected(_ selected: Int, frequency count: Int) {
  
        self.turntableView.startRotationAnimation(CGFloat(selected))
    }
    //转盘关闭
    func turntableClose() {
   
        self.turntableView.closeButtonClict()
    }
   // #pragma mark 双击视频view
    func objectDidDragged(_ vc: TF1to16VideoAreaCell) {
        self.double_tap_to_full_screenUI(vc.model!)
    }
    func dealSingleTap(_ vc: TF1to16VideoAreaCell) {
        
    }

    func objectKeyNoteDidDragged(_ vc: TFkeyNoteView) {
        
  
        if (self.talkfunSDK.liveStatus != TalkfunLiveStatusStart) {
            return;
        }
  
                 if (vc.frame.size.height != self.superView.bounds.size.height) {

                     self.keynoteLive.keynoteView.frame = self.superView.bounds;
                     self.talkfunSDK.scalingMode = .aspectFit
                     self.keynoteLive.keynoteView.isfullClick = true
                     self.keynoteLive.keynoteView.updateVideoFrame()
                     self.keynoteLive.keynoteView.zoomButton.isHidden = false
                     self.keynoteLive.keynoteView.nameBackgroudView.isHidden = true
                 }else{
                     self.keynoteLive.keynoteView.isfullClick = false

                     self.talkfunSDK.scalingMode = .fill;
                     self.updetaKeyfalseteViewFrame(true)
                     self.keynoteLive.keynoteView.updateVideoFrame()
                     self.keynoteLive.keynoteView.zoomButton.isHidden = true
                     self.keynoteLive.keynoteView.nameBackgroudView.isHidden = false
                 }
       
    }

    //双击了 -- 画廊模式
    
    func GalleryobjectDidDragged(_ vc: TF1to16VideoAreaCell) {
 
        if (self.talkfunSDK.liveStatus != TalkfunLiveStatusStart) {
            return
        }
 
        if (self.galleryLive.videoArea.videoSessionsArray.count == 1) {
                return
            }
            
        self.double_tap_to_full_screenUI(vc.model!)
        self.talkfunSDK.scalingMode = .aspectFit

        
    }
    //单击
    func GallerydealSingleTap(_ vc: TF1to16VideoAreaCell) {
        
    }

    /**
     * @画板载入课件
     */
    func loadCourseware() {
        self.courseLive.drawing.defaultDrawing1vN.isHidden = true
    }
 
  //  #pragma mark 桌面分享开始--结束
    /**
    *当前是不是桌面模式
    */
    func isRealDesktop(_ isDesktop: Bool) {
   

        self.desktopContainerView.isHidden = !isDesktop
        if (isDesktop) {

            self.courseLive.drawing.defaultDrawing1vN.isHidden = true
            
            self.desktopContainerView.isHidden = false
            self.courseLive.drawing.whiteboardTools.isHidden = true
        
        }else{
            if (self.talkfunSDK.isLoadCourseware == false) {
                self.courseLive.drawing.defaultDrawing1vN.isHidden = false
            }
            
            self.desktopContainerView.isHidden = true

            if (self.talkfunSDK.rtcManager.whiteboard.graffiti) {
                if (self.mediaContainerView.docType != 4) {
                    self.courseLive.drawing.whiteboardTools.isHidden = false

                }
               
            }
        }
        
    }
    /**
    *     抽奖开始
    */
//    func lotteryStart() {
//
//        self.lottery.isHidden = false
//        self.listOfWinners.isHidden = true
//        self.lottery.startScrollBottomToTopWithSpace()
//        self.superView.addSubview(self.below)
//
//    }
    /**
        停止抽奖
        String pub; 公布中奖人 0：未公布 1： 公布
        String roomid; //房间id
        String launch_xid;  中奖人的xid
        String time; 时间
        String liveid;  直播id
        String course_id;课程id
        String uid;
        昵称
        String nickname;
        String launch_nickname;

       currentXid  当前账号的xid
      */
    
//    func lotteryStop(_ lotteryResult: [Any], pub: String, gid: String, currentXid xid: String) {
//
//        self.listOfWinners.reloadData(lotteryResult as NSArray, xid:xid)
//        self.lottery.isHidden = true
//        self.listOfWinners.isHidden = false
//        self.lottery.stopToEmpty()
//
//    }
    func isShowvCameraView(_ isShow:Bool)
    {

        let drawingX:CGFloat = 0
        var drawingY = self.courseLive.videoArea.frame.maxY
        let drawingW = self.courseLive.view.frame.size.width

        if (isShow )
        {
            drawingY = 0
        }
         let drawingH = self.superView.frame.size.height - drawingY
        
        if (isShow&&self.talkfunSDK.realDesktop == false) {

        self.courseLive.videoArea.isHidden = true
        }else{

        self.courseLive.videoArea.isHidden = false
       
        }
        self.courseLive.drawing.frame = CGRect(x: drawingX, y: drawingY, width: drawingW, height: drawingH)
      
        //大班互动才动态变
        if (self.modetype == "6") {
           self.courseLive.drawing.tempFrame =  self.courseLive.drawing.frame
        }

    }
    //#pragma mark  切换主讲人
    func onSwitchZhujiang(_ obj: TFKeyNote) {
   
        let xid = obj.xid as String
        let status = obj.status as NSInteger
        self.zhujiangStatus = status

        let model1 = self.keynoteLive.keynoteView.videoArr[0] as! TalkfunRtcModel
   
         if (status == 0) {
             self.zhujiangxid = "1"
             
             for i in 0..<self.keynoteLive.videoArea.videoSessionsArray.count {
           
                 let model = self.keynoteLive.videoArea.videoSessionsArray[i] as! TalkfunRtcModel
                 if (model.role == .roleeUser) {

                 } else {
                     self.keynoteLive.videoArea.videoSessionsArray.replaceObject(at: i, with: model1)
               
                     self.keynoteLive.videoArea.collectionView!.reloadData()
                     self.keynoteLive.keynoteView.videoArr.removeAllObjects()
                     
                     self.keynoteLive.keynoteView.updetaModel(model, lianmai: true)
                 }

               }

            }
           else if (status==1) {
               self.zhujiangxid = xid
               for i in 0..<self.keynoteLive.videoArea.videoSessionsArray.count {
                let model = self.keynoteLive.videoArea.videoSessionsArray[i] as! TalkfunRtcModel

                    if (model.xid  == xid ) {
                        self.keynoteLive.videoArea.videoSessionsArray.replaceObject(at: i, with: model1)
                      
                        self.keynoteLive.videoArea.collectionView!.reloadData()
                        self.keynoteLive.keynoteView.videoArr.removeAllObjects()
                        self.keynoteLive.keynoteView.updetaModel(model, lianmai: true)
                      
                    }
                }

            }
      

    }

 //   #pragma mark 切换课程模式
    func onSwitchVideoMode(_ mode: TalkfunPageMode) {
   
        if (mode == TalkfunPageModeCourse)
        {
            self.addCourseMode()
            self.courseLive.videoArea.videoSessionsArray.removeAllObjects()
            self.mediaContainerView.isHidden = false
            if (self.talkfunSDK.liveStatus == TalkfunLiveStatusStop)
            {
                self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "0")

                self.isShowvCameraView(false)
                self.navigationBar.barrage.tag = 0
                self.courseLive.drawing.podiumConfiguration.isHidden = true
                self.courseLive.drawing.whiteboardTools.isHidden = true
                
                self.navigationBar.barrage.setImage(UIImage.init(named: "HTLive.bundle/barrageOff1vN"), for: .normal)
                
                
                self.courseLive.drawing.defaultDrawing1vN.isHidden = true
                self.courseLive.videoArea.isHidden = true
                self.navigationBar.setButton.isHidden = true
                self.courseLive.drawing.isFullButton.isHidden = true
                self.navigationBar.setButtonTools.isHidden = false
                
                
                self.courseLive.drawing.podiumConfiguration.podiumStatus(.noApply)
                self.promptTool.backgroundImage.isHidden = false
                self.promptTool.loading.isHidden = true;
                self.promptTool.backgroundImage.isHidden = false
//                self.promptTool.backgroundImage.backgroundColor =  self.superView.backgroundColor;
                self.promptTool.backgroundImage.imageView.isHidden = false
                self.promptTool.backgroundImage.imageView.image = UIImage.init(named: "HTLive.bundle/livestoped")
                
                
                
                self.promptTool.backgroundImage.label.text = "直播已结束"
                self.promptTool.backgroundImage.label.isHidden = false
                self.navigationBar.time.isHidden = true;
                self.navigationBar.title.text = ""
                self.courseLive.videoArea.liveStop()

                self.talkfunSDK.rtcManager.whiteboard.setLineRgbColor(UIColor(red: 0.99, green: 0.18, blue: 0.02, alpha: 1))
                
                
                self.keyboardWillHide1()
                self.mediaContainerView.isMediaApplicatea = false
             
            }
            else if (self.talkfunSDK.liveStatus == TalkfunLiveStatusWait)
            {
                self.promptTool.loading.isHidden = true;
                
                self.promptTool.backgroundImage.imageView.isHidden = false
                self.promptTool.backgroundImage.label.isHidden = false
                self.promptTool.backgroundImage.imageView.image = UIImage.init(named: "HTLive.bundle/livestoped")
                
                
                self.promptTool.backgroundImage.label.text = "直播未开始"
                
         
                self.courseLive.videoArea.isHidden = true
                self.promptTool.backgroundImage.isHidden = false
                
                self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
                
            }
            else {
                
                if (self.talkfunSDK.rtcManager.applyStatus == .allow) {
                    
                    self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
                    
                   
                } else {
                    
                    self.navigationBar.takeThePodium(false, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
                    
                   
                }

                //获取上讲台的数据源
                if (self.galleryLive.videoArea.videoSessionsArray.count > 0) {
                    self.courseLive.videoArea.videoSessionsArray.addObjects(from: self.galleryLive.videoArea.videoSessionsArray as! [Any])
                    
                  
                    self.galleryLive.view.removeFromSuperview()
//                    self.galleryLive = nil
                } else {
                    self.courseLive.videoArea.videoSessionsArray.addObjects(from: self.keynoteLive.keynoteView.videoArr as! [Any])
    
                    self.keynoteLive.view.removeFromSuperview()
//                    self.keynoteLive = nil
                }
     
                self.courseLive.videoArea.isHidden = false
                if (self.talkfunSDK.isLoadCourseware == false) {
                    self.courseLive.drawing.defaultDrawing1vN.isHidden = false
                }
                if (self.drawPower) {
                    self.courseLive.drawing.whiteboardTools.isHidden = false
                } else {
                    self.courseLive.drawing.whiteboardTools.isHidden = true
                }
                
                self.promptTool.loading.isHidden = true
                self.courseLive.videoArea.isHidden = false
                
                for i in 0..<self.courseLive.videoArea.videoSessionsArray.count {
                
              
                    let model = self.courseLive.videoArea.videoSessionsArray[i] as! TalkfunRtcModel
                    
                    self.courseLive.videoArea.updateModel(model)
                    
                }
            }
            

        }
        else if (mode == TalkfunPageModeKeyNote)
        {

            self.addkeynoteMode()
            self.keynoteLive.videoArea.videoSessionsArray.removeAllObjects()
            self.mediaContainerView.isHidden = true
            self.promptTool.loading.isHidden = true
            if (self.talkfunSDK.liveStatus == TalkfunLiveStatusStop) {
                
                self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
 
                self.keynoteLive.videoArea.liveStop()
                self.promptTool.backgroundImage.isHidden = true
                self.keynoteLive.keynoteView.video.isHidden = true
                self.keynoteLive.keynoteView.noStartImg.isHidden = false
                self.keynoteLive.keynoteView.nameImg.isHidden = true
                self.keynoteLive.videoArea.noUserLianmai.isHidden = true
                self.keynoteLive.keynoteView.nameLab.isHidden = false
                self.keynoteLive.keynoteView.nameBackgroudView.isHidden = false
                self.keynoteLive.keynoteView.nameLab.text = self.zhuboName;
                self.keynoteLive.keynoteView.updateNameFram(self.zhuboName, backcolor:true)
     
            }
            else if (self.talkfunSDK.liveStatus == TalkfunLiveStatusWait) {
                self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
                self.promptTool.backgroundImage.isHidden = true
                self.keynoteLive.keynoteView.video.isHidden = true
                self.keynoteLive.keynoteView.noStartImg.isHidden = false
                self.keynoteLive.keynoteView.nameImg.isHidden = true
                self.keynoteLive.keynoteView.nameBackgroudView.backgroundColor = UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 0.8)

                self.keynoteLive.videoArea.noUserLianmai.isHidden = true

                self.updatekeyNoteNameFrame(self.zhuboName)
               
                self.keynoteLive.keynoteView.nameLab.text = self.zhuboName;
            }
            else if (self.talkfunSDK.liveStatus==TalkfunLiveStatusStart){
                self.keynoteLive.keynoteView.video.isHidden = false
                if (self.talkfunSDK.rtcManager.rtcStatus == .open) {
                    self.keynoteLive.videoArea.noUserLianmai.isHidden = false
                    self.keynoteLive.videoArea.isHidden = false
                    self.updetaKeyfalseteViewFrame(true)
                      
                    self.keynoteLive.keynoteView.updateVideoFrame()
                }
                else {
                    self.keynoteLive.videoArea.noUserLianmai.isHidden = true
                    self.keynoteLive.videoArea.isHidden = true
                    self.updetaKeyfalseteViewFrame(false)
                    self.keynoteLive.keynoteView.updateVideoFrame()
                }
                
                if (self.talkfunSDK.rtcManager.applyStatus == .allow) {
                    self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
                    
                   
                } else {
                    self.navigationBar.takeThePodium(false, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
                    
                  
                }
                //获取上讲台的数据源
                if (self.courseLive.videoArea.videoSessionsArray.count > 0) {
                    self.keynoteLive.videoArea.videoSessionsArray.addObjects(from: self.courseLive.videoArea.videoSessionsArray as! [Any])
               
                    self.courseLive.view.removeFromSuperview()
//                    self.courseLive = nil;
                } else {
                    self.keynoteLive.videoArea.videoSessionsArray.addObjects(from: self.galleryLive.videoArea.videoSessionsArray as! [Any])
                    self.galleryLive.view.removeFromSuperview()
//                    self.galleryLive = nil;
                }
                if (self.keynoteLive.videoArea.videoSessionsArray.count > 1) {
                    self.updetaKeyfalseteViewFrame(true)
                }
                self.keynoteLive.videoArea.isHidden = false
                
                let copyVideoSessionsArray = NSMutableArray.init(array: self.keynoteLive.videoArea.videoSessionsArray)
                
                for i in 0..<copyVideoSessionsArray.count {
            
                    let model = copyVideoSessionsArray[i] as! TalkfunRtcModel
                        
                    self.keynoteLive.videoArea.removeAdminModel(model)
                    self.keynoteLive.keynoteView.updetaModel(model, lianmai:false)
                    
                }

            }
         
          
        }
        else if (mode == TalkfunPageModeGallery)
        {

            self.addgalleryMode()
            self.galleryLive.videoArea.videoSessionsArray.removeAllObjects()
            self.mediaContainerView.isHidden = true
            //获取上讲台的数据源
            if (self.talkfunSDK.liveStatus == TalkfunLiveStatusStop) {
                self.promptTool.backgroundImage.isHidden = true
                self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
                
              
                self.navigationBar.barrage.tag = 0
                self.navigationBar.time.isHidden = true
                self.navigationBar.title.text = ""
                self.galleryLive.videoArea.liveStop()
                self.galleryLive.videoArea.isHidden = true
                self.galleryLive.noStartView.isHidden = false
                
           
                let rect = TalkfunSharedTools.getRectWith(self.zhuboName, size: CGSize(width: 200, height: 20), fontSize: 11)
                                                    
                self.galleryLive.nameLab.frame = CGRect(x: 5, y: self.galleryLive.noStartView.frame.size.height-20, width: rect.size.width+10, height: 20)
                
              
                self.galleryLive.nameLab.text = self.zhuboName
            }
            else if (self.talkfunSDK.liveStatus == TalkfunLiveStatusWait)
            {
                self.promptTool.loading.isHidden = true;
                self.promptTool.backgroundImage.isHidden = true
               
                self.keynoteLive.videoArea.isHidden = true
                self.galleryLive.videoArea.isHidden = true
                self.galleryLive.noStartView.isHidden = false
                let rect = TalkfunSharedTools.getRectWith(self.zhuboName, size: CGSize(width: 200, height: 20), fontSize: 11)
                self.galleryLive.nameLab.frame = CGRect(x: 5, y: self.galleryLive.noStartView.frame.size.height-20, width: rect.size.width+10, height: 20)
               
                self.galleryLive.nameLab.text = self.zhuboName
               
            }
            else {
                
                if (self.talkfunSDK.rtcManager.applyStatus == .allow)
                {
                    self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "2")
                    
                   
                }
                else {
                    
                    self.navigationBar.takeThePodium(false, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "2")
                 
                }
                
                if (self.courseLive.videoArea.videoSessionsArray.count > 0) {
                    self.galleryLive.videoArea.videoSessionsArray.addObjects(from: self.courseLive.videoArea.videoSessionsArray as! [Any])
                
                    self.courseLive.view.removeFromSuperview()
//                     self.courseLive = nil;
                } else {
                    self.galleryLive.videoArea.videoSessionsArray.addObjects(from: self.keynoteLive.keynoteView.videoArr as! [Any])
            
                    self.galleryLive.videoArea.videoSessionsArray.addObjects(from: self.keynoteLive.videoArea.videoSessionsArray as! [Any])
                
          
                   
                    self.keynoteLive.view.removeFromSuperview()
//                    self.keynoteLive = nil;
                }
               
               
                self.galleryLive.videoArea.isHidden = false
            }
            
         
        }

    }
  //  #pragma mark  被踢出房间
    func onMemberKick() {

        let alert = UIAlertController.init(title: "", message: "你被退出房间了", preferredStyle:.alert)
     
        let otherAction:UIAlertAction = UIAlertAction.init(title: "退出", style: .default) { action in
            self.exit()
        }
        alert.addAction(otherAction)
        if  UIDevice.current.model.hasPrefix("iPad") {
            alert.popoverPresentationController?.sourceView = self.superView
            alert.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
        }
        
        self.present(alert, animated: true)
    }
    // TFFullViewDelegate  全屏缩小的代理
    func objectFullViewDidDragged(_ vc:TFFullView)
    {
        self.talkfunSDK.scalingMode = .fill
        vc.model.isFull = false
        self.fullView.isHidden = true
        self.fullView.removeFromSuperview()
    
            for i in 0..<vc.model.view.subviews.count {

               let subView = vc.model.view.subviews[i] as UIView
                subView.frame = vc.bounds
            }
        


        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.courseLive.videoArea.isFull = false
            if self.courseLive.videoArea.collectionView != nil {
                self.courseLive.videoArea.collectionView!.reloadData()
            }
            

        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
            self.keynoteLive.videoArea.isFull = false
            
            if self.keynoteLive.videoArea.collectionView != nil {
            self.keynoteLive.videoArea.collectionView!.reloadData()
                
            }
         
        } else  if (self.talkfunSDK.pageMode == TalkfunPageModeGallery){
            self.galleryLive.videoArea.isFull = false
            if self.galleryLive.videoArea.collectionView != nil {
                self.galleryLive.videoArea.collectionView!.reloadData()
            }
            
        }
    }
   // #pragma mark  直播间状态
    func onLiveStatus(_ status: TalkfunLiveStatus) {

        if (status == TalkfunLiveStatusWait)
        {
       
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.promptTool.loading.isHidden = true
            
            self.promptTool.backgroundImage.imageView.isHidden = false
            self.promptTool.backgroundImage.label.isHidden = false
            
            self.promptTool.backgroundImage.imageView.image = UIImage.init(named: "HTLive.bundle/livestoped")
            
            self.promptTool.backgroundImage.label.text = "直播未开始"
            self.courseLive.videoArea.isHidden = true
            
            self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
  
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
            self.promptTool.backgroundImage.isHidden = true
            self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
            self.navigationBar.barrage.tag = 0
            self.navigationBar.time.isHidden = true
            self.navigationBar.title.text = ""
            self.keynoteLive.videoArea.liveStop()
            self.keynoteLive.keynoteView.video.isHidden = true
            self.keynoteLive.keynoteView.noStartImg.isHidden = false
            self.keynoteLive.keynoteView.nameImg.isHidden = true
            self.keynoteLive.keynoteView.nameBackgroudView.backgroundColor = UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 0.8)

            self.keynoteLive.videoArea.noUserLianmai.isHidden = true

            self.updatekeyNoteNameFrame(self.zhuboName)
            self.keyboardWillHide1()
         
            self.keynoteLive.videoArea.isHidden = true
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
                self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "2")
            self.galleryLive.videoArea.isHidden = true
            self.galleryLive.noStartView.isHidden = false
            
            
            let rect = TalkfunSharedTools.getRectWith(self.zhuboName, size: CGSize(width: 200, height: 20), fontSize: 11)
                           
            self.galleryLive.nameLab.frame = CGRect(x: 5, y: self.galleryLive.noStartView.frame.size.height-20, width: rect.size.width+10, height: 20)
                  
            self.galleryLive.nameLab.text = self.zhuboName
           
        }

        if (self.navigationBar.showChat==false) {
            self.navigationBar.inputField.isHidden = true
        }
        else {
            if (self.chatDisable==false && self.chatDisableAll == false) {
                if (self.mod_beforeclass_live) {
                    self.navigationBar.inputField.isHidden = false
                } else {
                    self.navigationBar.inputField.isHidden = true
                }
            } else {
                self.navigationBar.inputField.isHidden = true
            }
            
        }
     
        }
        else   if (status == TalkfunLiveStatusStart)
            
        {
     
            if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
                self.courseLive.drawing.view.isHidden = false
                self.promptTool.backgroundImage.backgroundColor = UIColor.clear
                self.scoreView.successBtton()
              
                if (self.talkfunSDK.rtcManager.rtcStatus == .open) {
                    self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
                    
               
                } else {
                   
                    self.navigationBar.takeThePodium(false, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
                }

                if (self.modetype == "6" && self.talkfunSDK.realDesktop == false)
                {
                    self.isShowvCameraView(true)
                }
                self.courseLive.drawing.podiumConfiguration.isHidden = false
                self.navigationBar.barrage.setImage(UIImage.init(named: "HTLive.bundle/barrage1vN"), for: .normal)
                
                self.navigationBar.barrage.tag = 1
               
                if (self.talkfunSDK.isLoadCourseware == false && self.talkfunSDK.realDesktop == false) {
                    self.courseLive.drawing.defaultDrawing1vN.isHidden = false
                }
                self.courseLive.videoArea.isHidden = false
              

                self.navigationBar.setButtonTools.isHidden = false
            }
            else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote)
            {
                if (self.talkfunSDK.rtcManager.rtcStatus == .open) {
                    
                    self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
                    
                
                } else {
                
                    self.navigationBar.takeThePodium(false, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
                }
                self.navigationBar.barrage.setImage(UIImage.init(named: "HTLive.bundle/barrage1vN"), for: .normal)
              
                self.navigationBar.barrage.tag = 1
                self.promptTool.loading.isHidden = true

                self.navigationBar.setButtonTools.isHidden = false
            }
            else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery)
            {
                if (self.talkfunSDK.rtcManager.rtcStatus == .open) {
                    self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "2")
                    
                   
                } else {
                   
                    self.navigationBar.takeThePodium(false, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "2")
                }
                self.navigationBar.barrage.setImage(UIImage.init(named: "HTLive.bundle/barrage1vN"), for: .normal)
                
                self.navigationBar.barrage.tag = 1
                self.promptTool.loading.isHidden = true

                self.navigationBar.setButtonTools.isHidden = false
                self.galleryLive.noStartView.isHidden = true
                self.galleryLive.videoArea.isHidden = false
            }

            self.navigationBar.title.text = self.talkfunSDK.roomInfo.title
            self.promptTool.loading.isHidden = true
                self.navigationBar.isFullButton.setImage(UIImage.init(named: "HTLive.bundle/fullScreen1vN"), for: .normal)
         
            self.promptTool.backgroundImage.isHidden = true
       
        }else{
            //抽奖
            self.lotteryVC?.view.removeFromSuperview()
            //转盘
            self.turntableView.closeButtonClict()
            
            self.talkfunSDK.rtcManager.rtcStatus = .close
            self.drawPower = false

                self.objectFullViewDidDragged(self.fullView)

                if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
                    self.courseLive.drawing.view.isHidden = true
                    

//                    if(self.talkfunSDK.evaluate && self.modetype == "6" && self.isScore == false){
//
//                               //配置
//                        self.scoreView.setScoreConfig(self.talkfunSDK.roomInfo.scoreConfig.valueList)
//                           //1.启动了评分
//                            self.isScore = true
//                               //TODO:评分提交
//                        self.scoreView.submit.addTarget(self, action: #selector(submitClick), for: .touchUpInside)
//                        
//                          
//                           //2.添加了评分
//                           self.superView.addSubview(self.scoreView)
//
//             
//                   }
                    self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
                   
                    self.isShowvCameraView(false)
                    self.courseLive.drawing.whiteboardTools.isHidden = true
                    self.navigationBar.barrage.tag = 0;
                    self.courseLive.drawing.podiumConfiguration.isHidden = true
                    self.navigationBar.barrage.setImage(UIImage.init(named: "HTLive.bundle/barrageOff1vN"), for: .normal)
                   
                    self.courseLive.drawing.defaultDrawing1vN.isHidden = true
                    self.courseLive.videoArea.isHidden = true
                    self.navigationBar.setButton.isHidden = true
                    self.courseLive.drawing.isFullButton.isHidden = true
                    self.navigationBar.setButtonTools.isHidden = false
                    self.courseLive.drawing.podiumConfiguration.podiumStatus(.noApply)
                    self.navigationBar.ondium.onPodium(.noApply)
                    self.promptTool.backgroundImage.isHidden = false
                    self.promptTool.loading.isHidden = true
                    self.promptTool.backgroundImage.isHidden = false
//                    self.promptTool.backgroundImage.backgroundColor =  self.superView.backgroundColor
                    self.promptTool.backgroundImage.imageView.isHidden = false
                    self.promptTool.backgroundImage.imageView.image = UIImage.init(named: "HTLive.bundle/livestoped")
                 
                    self.promptTool.backgroundImage.label.text = "直播已结束"
                    self.promptTool.backgroundImage.label.isHidden = false
                    self.navigationBar.time.isHidden = true
                    self.navigationBar.title.text = ""
                    self.courseLive.videoArea.liveStop()

                    self.talkfunSDK.rtcManager.whiteboard.setLineRgbColor(UIColor(red: 0.99, green: 0.18, blue: 0.02, alpha: 1))
                    
                  
                    //申请上讲台   隐藏
                    self.courseLive.drawing.podiumConfiguration.onPodium.isHidden = true
                    self.keyboardWillHide1()
                    self.mediaContainerView.isMediaApplicatea = false
                } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
                    self.promptTool.backgroundImage.isHidden = true
                    self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
                    
                   
                    self.navigationBar.barrage.tag = 0
                    self.navigationBar.time.isHidden = true
                    self.navigationBar.title.text = ""
                    self.keynoteLive.videoArea.liveStop()
                    self.keynoteLive.keynoteView.video.isHidden = true
                    self.keynoteLive.keynoteView.noStartImg.isHidden = false
                    self.keynoteLive.keynoteView.nameImg.isHidden = true
                    self.keynoteLive.videoArea.noUserLianmai.isHidden = true
                    self.navigationBar.ondium.onPodium(.noApply)
                 
                    self.keyboardWillHide1()
                    if (self.smallType  == "2") {
                        
                        self.updetaKeyfalseteViewFrame(false)

                    } else {
                        self.updetaKeyfalseteViewFrame(false)
                        self.updatekeyNoteNameFrame(self.zhuboName)
                    }
                        self.keynoteLive.keynoteView.updateNameFram(self.zhuboName, backcolor: true)
                        
                 
                    self.promptTool.backgroundImage.isHidden = true
                } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
                    self.promptTool.backgroundImage.isHidden = true
                  
                    self.navigationBar.takeThePodium(false, liveStart: false, modetype: self.modetype, smallType: self.smallType, shipinType: "2")
                    
                    self.navigationBar.ondium.onPodium(.noApply)
                    self.navigationBar.barrage.tag = 0;
                    self.navigationBar.time.isHidden = true
                    self.navigationBar.title.text = ""
                    self.galleryLive.videoArea.liveStop()
                    self.galleryLive.videoArea.isHidden = true
                    self.galleryLive.noStartView.isHidden = false
             
               let rect = TalkfunSharedTools.getRectWith(self.zhuboName, size: CGSize(width: 200, height: 20), fontSize: 11)
                                   
                    self.galleryLive.nameLab.frame = CGRect(x: 5, y: self.galleryLive.noStartView.frame.size.height-20, width: rect.size.width+10, height: 20)
                    
               
                    self.galleryLive.nameLab.text = self.zhuboName
                    self.promptTool.backgroundImage.isHidden = true
                }
          
                
                if (self.navigationBar.showChat==false) {
                    self.navigationBar.inputField.isHidden = true
                } else {
                    //单体的禁言比课前课后的禁言配置优先
                    if (self.chatDisable==false && self.chatDisableAll == false) {
                        if (self.mod_afterclass_live) {
                            self.navigationBar.inputField.isHidden = false
                        } else {
                            self.navigationBar.inputField.isHidden = true
                        }
                    } else {
                        self.navigationBar.inputField.isHidden = true
                    }
                    
                }
        }
        
    }
   // #pragma mark true :单体禁言  false:单体解除禁言
    func onChatDisable(_ state: Bool) {

        if (state) {
        self.chatDisable = true
        self.TF_toast("你已被禁言")
        self.navigationBar.inputField.isHidden = true
         
        }else{
          
            self.chatDisable = false
            self.TF_toast("禁言已解除")
            self.chatArea.chatIsDisable(false)
        //直播已结束
        if (self.talkfunSDK.liveStatus == TalkfunLiveStatusStop) {
            if (self.chatDisableAll == false && self.mod_afterclass_live) {
                self.navigationBar.inputField.isHidden = false
            } else {
                self.navigationBar.inputField.isHidden = true
            }
        } else if (self.talkfunSDK.liveStatus == TalkfunLiveStatusWait) {
            //直播未开始
            if (self.chatDisableAll == false && self.mod_beforeclass_live) {
                self.navigationBar.inputField.isHidden = false
            } else {
                self.navigationBar.inputField.isHidden = true
            }
        } else {
            if (self.chatDisableAll == false) {
                self.navigationBar.inputField.isHidden = false
            } else {
                self.navigationBar.inputField.isHidden = true
            }
        }
            
        }
    }
   // #pragma mark true :全体禁言 false:全体解除禁言
    func onChatDisableAll(_ state: Bool) {
 
        //禁言
        if(state==true){
            self.TF_toast("你已被禁言")
      
            self.navigationBar.inputField.isHidden = true
            self.chatDisableAll = true
            //能说话
        }else if(state == false){

            if (self.navigationBar.inputField.isHidden) {
                self.TF_toast("禁言已解除")
              
                
            }
            
                 self.chatDisableAll = false
                 //直播已结束
                 if (self.talkfunSDK.liveStatus == TalkfunLiveStatusStop) {
                     if (self.chatDisable == false && self.mod_afterclass_live) {
                         self.navigationBar.inputField.isHidden = false
                     } else {
                         self.navigationBar.inputField.isHidden = true
                     }
                 } else if (self.talkfunSDK.liveStatus == TalkfunLiveStatusWait) {
                     //直播未开始
                     if (self.chatDisable == false && self.mod_afterclass_live) {
                         self.navigationBar.inputField.isHidden = false
                     } else {
                         self.navigationBar.inputField.isHidden = true
                     }
                 } else {
                     if (self.chatDisable == false) {
                         self.navigationBar.inputField.isHidden = false
                     } else {
                         self.navigationBar.inputField.isHidden = true
                     }
                 }
            }
    }
   // #pragma mark ---------- 进入房间事件的回调
    func onEnterRoom(_ code: Int, result error: TFError)
    {

        //TODO: 设置
        if (self.setButtonView.frame.width==0) {
            self.setButtonView.initUI(frame: CGRect(x: self.superView.frame.size.width+220, y: 0, width: self.superView.frame.size.width, height: self.superView.frame.size.height), xid: self.talkfunSDK.xid)
           

        self.setButtonView.xid = self.talkfunSDK.xid
        self.setButtonView.delegate2 = self
        self.setButtonView.contentSize = CGSize(width: 0, height: 400)
 
        self.superView.addSubview(self.setButtonView)

        }
        
        if ( code == 0 ) {
            self.smallType =  String.init(format: "%i", self.talkfunSDK.roomInfo.usercamera.smallType)
            
            self.barrageRender.bulletSwitch = TalkfunSettingToolsManager.shared().getToolsSwitchBarrage(self.talkfunSDK.xid)
            
            self.zhuboName = self.talkfunSDK.roomInfo.zhuboInfo.nickname
         
            if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
                if (self.isRefresh) {
                     self.addCourseMode()
                }
              
            } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
                if (self.isRefresh) {
                self.addkeynoteMode()
             
                self.keynoteLive.keynoteView.nameLab.text = self.zhuboName;
                self.updatekeyNoteNameFrame(self.zhuboName)
                
                }
                
            } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
                if (self.isRefresh) {
                self.addgalleryMode()
                }
              
            }
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1) {

           
                self.isShow = true
            }
            
            self.promptTool.loading.isHidden = true
            


            let isOn = TalkfunSettingToolsManager.shared().getToolsLanguageChange(self.talkfunSDK.xid)
          
           if (isOn) {
               
               TFChangeLanguage.sharedInstance.setLanguage(language: "en")
              
               self.updataLanguage(isOn)
           }
          
            //课前聊天
            self.mod_beforeclass_live = self.talkfunSDK.roomInfo.mod_beforeclass_live.config.chat.enable
            //课后聊天
            self.mod_afterclass_live = self.talkfunSDK.roomInfo.mod_afterclass_live.config.chat.enable
            self.navigationBar.title.text = self.talkfunSDK.roomInfo.title
            //设置聊天
            self.navigationBar.showChat = self.talkfunSDK.roomInfo.mod_text_live.config.chat.enable
            //设置问答
            self.navigationBar.showQa = self.talkfunSDK.roomInfo.mod_text_live.config.qa.enable
            //设置投票
            self.navigationBar.showVote = self.talkfunSDK.roomInfo.mod_zhuboaction_live.config.vote.enable
            self.navigationBar.showCourseDown = true
            if (self.talkfunSDK.roomInfo.mod_text_live.config.qa.enable) {
                self.qskQuestionsView.textField.isHidden = false
            } else {
                self.qskQuestionsView.textField.isHidden = true
            }
            self.navigationBar.showOndium = self.talkfunSDK.roomInfo.usercamera.apply
          
            
            if (self.talkfunSDK.roomInfo.mod_liveinfo_live.config.title.enable==true) {
                self.navigationBar.title.isHidden = false
            } else {
                self.navigationBar.title.isHidden = true
            }

            if (self.talkfunSDK.roomInfo.zhujiang.enable == false) {
            self.zhujiangStatus = 0
            }else{
            self.zhujiangStatus = self.talkfunSDK.roomInfo.zhujiang.status
            self.zhujiangxid = self.talkfunSDK.roomInfo.zhujiang.xid
            }

            if (self.turntableView.isClose) {
                self.turntableView.isHidden = false
            }
            
            self.courseLive.drawing.podiumConfiguration.podiumStatus(self.talkfunSDK.rtcManager.applyStatus)

            //获取提问列表
            self.getAListOfQuestions()

            //可用于投票判断是不是自己
            self.votingArea.voteDetail.uid = self.talkfunSDK.uid;
            self.chatArea.uid = self.talkfunSDK.uid;
        
            //只有显示的时候才请求课件数据 课件下载开关
            self.onDocumentDownload(self.talkfunSDK.roomInfo.mod_document_download_live.enable)

        }else{
            self.TF_toast(error.msg)
            
        }
    }
 //   #pragma mark 讲台开启
 func rtcOpen() 
    {
        
            if(self.talkfunSDK.rtcManager.applyStatus == .noApply && self.smallType != "2"){
                self.TF_toast("连麦功能已开启")
              
            }

            self.navigationBar.ondium.onPodium(.noApply)
            
        //没开启自动上讲台
        if(self.talkfunSDK.rtcManager.autoUp == false && self.talkfunSDK.rtcManager.allowApply){

            self.navigationBar.ondium.isHidden = false
            
        }
        
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "0")
            
        } else  if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
            self.talkfunSDK.rtcManager.rtcStatus = .open
            
            self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
          
            self.keynoteLive.videoArea.noUserLianmai.isHidden = false
            self.keynoteLive.videoArea.isHidden = false
            self.updetaKeyfalseteViewFrame(true)
              
            self.keynoteLive.keynoteView.updateVideoFrame()
            
        } else  if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
            self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "2")
        }
                                    
    }
   // #pragma mark 讲台关闭
  func rtcClose()
    {
            self.TF_toast("连麦功能已关闭")
            
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.rtcInvitecancel()
            self.courseLive.drawing.podiumConfiguration.podiumStatus(.noApply)
            
            //所有学生下讲台
            self.courseLive.videoArea.allTheStudentsWalkedOffThePodium()
            self.courseLive.drawing.podiumConfiguration.onPodium.isHidden = true
            
            self.courseLive.drawing.whiteboardTools.isHidden = true
            self.navigationBar.takeThePodium(false, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "0")

        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote){
            self.rtcInvitecancel()
          
            //所有学生下讲台
            self.keynoteLive.videoArea.allTheStudentsWalkedOffThePodium()
            self.navigationBar.takeThePodium(false, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "1")
            
            self.keynoteLive.videoArea.noUserLianmai.isHidden = true
            self.keynoteLive.videoArea.isHidden = true
            self.talkfunSDK.rtcManager.rtcStatus = .close
            self.updetaKeyfalseteViewFrame(false)
                
            self.keynoteLive.keynoteView.updateVideoFrame()

        } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery){
            self.rtcInvitecancel()
           
            //所有学生下讲台
                
            self.galleryLive.videoArea.allTheStudentsWalkedOffThePodium()
            self.navigationBar.takeThePodium(false, liveStart: true, modetype: self.modetype, smallType: self.smallType, shipinType: "2")
           
        }
 
    }
   // #pragma mark 允许上讲台
 func rtcUp(_ model: TalkfunRtcModel)
    {
        if(model.isMe){

            if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
                self.navigationBar.takeThePodium(true, liveStart: true, modetype:self.modetype, smallType: self.smallType, shipinType: "0")
                
                
            } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
                self.navigationBar.takeThePodium(true, liveStart: true, modetype :self.modetype, smallType: self.smallType, shipinType: "1")
                
            } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
                self.navigationBar.takeThePodium(true, liveStart: true, modetype:self.modetype, smallType: self.smallType, shipinType: "2")
            }
            self.navigationBar.switchButton.setImage(UIImage.init(named: "HTLive.bundle/bigPpt"), for: .normal)
      
        if (self.talkfunSDK.isLoadCourseware == false) {
            self.courseLive.drawing.defaultDrawing1vN.isHidden = false
        }
        self.isShowOtherArea(false)
        
            self.navigationBar.ondium.onPodium(.allow)
            
            self.courseLive.drawing.podiumConfiguration.podiumStatus(.allow)
            
            self.navigationBar.updateModel(model)

        }
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
         
            self.courseLive.videoArea.updateModel(model)
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
           
            if (self.zhujiangStatus == 0) {
                if (model.role == .roleeUser) {
                    
                    self.keynoteLive.videoArea.updateModel(model)
                    self.keynoteLive.videoArea.noUserLianmai.isHidden = true
                    self.keynoteLive.videoArea.isHidden = false
                    self.updetaKeyfalseteViewFrame(true)
                   
                } else {
               
                    self.keynoteLive.videoArea.removeAdminModel(model)

                    self.keynoteLive.keynoteView.updetaModel(model, lianmai:false)

                }
            } else {
                
                if (model.xid  == self.zhujiangxid ) {
                    self.keynoteLive.videoArea.removeAdminModel(model)

                    self.keynoteLive.keynoteView.updetaModel(model, lianmai:false)
                } else {
                    self.keynoteLive.videoArea.updateModel(model)
                    self.keynoteLive.videoArea.noUserLianmai.isHidden = true
                    self.keynoteLive.videoArea.isHidden = false
                    self.updetaKeyfalseteViewFrame(true)
                }
            }
         
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
           
            self.galleryLive.videoArea.updateModel(model)
        }
    }
   // #pragma mark 拒绝用户上讲台
  func rtcRejectapply()
    {
        
    self.TF_toast("你的连麦申请被取消")
                        
    self.courseLive.drawing.podiumConfiguration.podiumStatus(.noApply)
                        
       self.navigationBar.ondium.onPodium(.noApply)
    }
   // #pragma mark 主动下讲台
    func rtcDown(_ model: TalkfunRtcModel)
    {
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
          
            self.courseLive.videoArea.removeFromVideo(model)
            
            self.courseLive.drawing.podiumConfiguration.podiumStatus(.noApply)
            if (model.isMe) {
                
            //1.不是1v6  2.已经上讲台
              if(self.smallType != "2" && self.talkfunSDK.rtcManager.allowApply == false){
                
                  self.courseLive.drawing.podiumConfiguration.onPodium.isHidden = true
                
               }
                self.TF_toast("你已取消连麦")
            

                self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.smallType, smallType: self.modetype, shipinType: "0")
                
                self.navigationBar.ondium.onPodium(.noApply)
                if(self.courseLive.drawing.whiteboardTools.isHidden==false){
                    //不是自己就隐藏
                    self.courseLive.drawing.whiteboardTools.isHidden = true
                }

            }
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
            self.keynoteLive.videoArea.removeFromVideo(model)
            self.keynoteLive.videoArea.noUserLianmai.isHidden = false
            if (model.isMe) {
                
            //1.不是1v6  2.已经上讲台
              if(self.smallType != "2" && self.talkfunSDK.rtcManager.allowApply == false){
                
                  self.courseLive.drawing.podiumConfiguration.onPodium.isHidden = true
                
               }
                self.TF_toast("你已取消连麦")
         
                self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.smallType, smallType: self.modetype, shipinType: "1")
                
                self.navigationBar.ondium.onPodium(.noApply)
            if (self.modetype == "6")
            {
            self.isShowvCameraView(true)
                
            }

         }
        }else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
            self.galleryLive.videoArea.removeFromVideo(model)
            
            self.courseLive.drawing.podiumConfiguration.podiumStatus(.noApply)
            if (model.isMe) {
                
            //1.不是1v6  2.已经上讲台
              if(self.smallType != "2"&&self.talkfunSDK.rtcManager.allowApply == false){
                
                  self.courseLive.drawing.podiumConfiguration.onPodium.isHidden = true
                
               }
                
                self.TF_toast("你已取消连麦")
                self.navigationBar.takeThePodium(true, liveStart: true, modetype: self.smallType, smallType: self.modetype, shipinType: "2")
                
                self.navigationBar.ondium.onPodium(.noApply)

         }
        }
                          
    }
   // #pragma mark 踢下讲台
    func rtcKick(_ model: TalkfunRtcModel)
    {
      self.rtcDown(model)
    }
  //  #pragma mark 用户离线
 func rtcOffline(_ model: TalkfunRtcModel)
    {
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.courseLive.videoArea.removeFromVideo(model)
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
            self.keynoteLive.videoArea.removeFromVideo(model)
            if (self.talkfunSDK.liveStatus == TalkfunLiveStatusStart) {
                self.keynoteLive.videoArea.noUserLianmai.isHidden = false
            } else {
                self.keynoteLive.videoArea.noUserLianmai.isHidden = true
            }
           
        }else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
            self.galleryLive.videoArea.removeFromVideo(model)
        }
      
    }
   // #pragma mark 打开摄像头
  func rtcOpenVideo(_ model: TalkfunRtcModel)
    {
        
        if (model.isFull) {
            self.fullView.view.isHidden = true
            self.fullView.backgroundColor = UIColor.clear
          
            self.fullView.insertSubview(model.view, at: 0)
            
            
        } else {
            self.fullView.view.isHidden = true
        }
        
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.courseLive.videoArea.updateModel(model)
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
            if (model.xid  == self.zhujiangxid  || model.role == .roleSpadmin) {
                self.keynoteLive.keynoteView.updetaModel(model,lianmai:true)
            } else {
                self.keynoteLive.videoArea.update1to1Model(model)
            }
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery){
            self.galleryLive.videoArea.updateModel(model)
        }
        if (model.isMe) {
           
                self.TF_toast("你可以使用摄像头了")
                TalkfunSettingToolsManager.shared().setZhuboCloseVideoStatus(false, xid: self.talkfunSDK.xid)
             
                self.setButtonView.hiddenBtn(true)

                self.navigationBar.updateModel(model)
            
        }
        
    }
 //   #pragma mark 关闭摄像头
   func rtcCloseVideo(_ model: TalkfunRtcModel)
    {
        
        if (model.isFull) {
            self.fullView.view.isHidden = false
            self.fullView.backgroundColor = UIColor.clear
        } else {
            self.fullView.view.isHidden = true
        }
        
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.courseLive.videoArea.updateModel(model)
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
           
            if (model.xid  == self.zhujiangxid  || model.role == .roleSpadmin) {
                self.keynoteLive.keynoteView.updetaModel(model, lianmai:true)
            } else {
                self.keynoteLive.videoArea.update1to1Model(model)
            }
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery){
            self.galleryLive.videoArea.updateModel(model)
        }

        if (model.isMe) {
            
            self.TF_toast("你的摄像头已关闭")
            
            TalkfunSettingToolsManager.shared().setZhuboCloseVideoStatus(true, xid: self.talkfunSDK.xid)
            self.setButtonView.hiddenBtn(false)
             
            self.navigationBar.updateModel(model)
            
        }

    }
  //  #pragma mark 打开麦克风
   func rtcOpenAudio(_ model: TalkfunRtcModel)
    {
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.courseLive.videoArea.updateModel(model)
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {

            if (model.xid  == self.zhujiangxid  || model.role == .roleSpadmin) {
                self.keynoteLive.keynoteView.updetaModel(model, lianmai:true)
            } else {
                self.keynoteLive.videoArea.update1to1Model(model)
            }
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery){
            self.galleryLive.videoArea.updateModel(model)
        }
        if (model.isMe) {
            
            self.TF_toast("你的麦克风已开启")
            
            self.navigationBar.updateModel(model)
            
            TalkfunSettingToolsManager.shared().setZhuboCloseAudioStatus(false, xid: self.talkfunSDK.xid)

            let switches = self.setButtonView.swicthArr[4] as! TFSwitch
             switches.isOn = true
           
        }
    }
   // #pragma mark 关闭麦克风
   func rtcCloseAudio(_ model: TalkfunRtcModel)
    {
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.courseLive.videoArea.updateModel(model)
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {

            if (model.xid  == self.zhujiangxid  || model.role == .roleSpadmin) {
                self.keynoteLive.keynoteView.updetaModel(model, lianmai:true)
            } else {
                self.keynoteLive.videoArea.update1to1Model(model)
            }
            
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery){
            self.galleryLive.videoArea.updateModel(model)
        }

    if (model.isMe) {
        
        self.TF_toast("你的麦克风已关闭")

        TalkfunSettingToolsManager.shared().setZhuboCloseAudioStatus(true, xid: self.talkfunSDK.xid)
        self.navigationBar.updateModel(model)
         
        let switches = self.setButtonView.swicthArr[4] as! TFSwitch
        switches.isOn = false
       
     }
    }
    //#pragma mark  打开涂鸦权限
   func rtcDrawEnable(_ model: TalkfunRtcModel)
    {
        self.drawPower =  true
         if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
             model.drawPower = true
             self.courseLive.videoArea.updateModel(model)

             if (model.isMe)
             {
                 self.TF_toast("你已被授予涂鸦权限")
            
                 self.courseLive.drawing.whiteboardTools.isHidden = false
             }else{
             //本来有涂鸦权限,现在切换给别人了
             if(self.courseLive.drawing.whiteboardTools.isHidden==false){
                 
                 self.TF_toast("你的涂鸦权限已被收回")
             //不是自己就隐藏
                 self.courseLive.drawing.whiteboardTools.isHidden = true
             }
             }
         } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {

             if (model.xid  == self.zhujiangxid ) {
                 self.keynoteLive.keynoteView.updetaModel(model, lianmai:true)
             } else {
                 self.keynoteLive.videoArea.update1to1Model(model)
             }
         } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery){
             self.galleryLive.videoArea.updateModel(model)
         }
       
    }
   // #pragma mark  关闭涂鸦权限
   func rtcDrawDisable(_ model: TalkfunRtcModel)
    {
        self.drawPower = false
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.courseLive.videoArea.updateModel(model)
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
            if (model.xid  == self.zhujiangxid ) {
                self.keynoteLive.keynoteView.updetaModel(model, lianmai:true)
            } else {
                self.keynoteLive.videoArea.update1to1Model(model)
            }
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery) {
            self.galleryLive.videoArea.updateModel(model)
        }

    if (model.isMe) {
        
        self.TF_toast("你的涂鸦权限已被收回")
        self.courseLive.drawing.whiteboardTools.isHidden = true
    }
        
    }
  //  #pragma mark 未连麦人员打开涂鸦权限
     func onMemberDrawEnable(_ model: TFMember) 
    {
        self.drawPower = true
        if (model.isMe) {
            
            self.TF_toast("你已被授予涂鸦权限")
            self.courseLive.drawing.whiteboardTools.isHidden = false
        }else
        {
            if (self.courseLive.drawing.whiteboardTools.isHidden == false) {
               
                self.TF_toast("你的涂鸦权限已被收回")
                self.courseLive.drawing.whiteboardTools.isHidden = true
            }
         
        }
    }
    //#pragma mark 未连麦人员关闭涂鸦权限
    func onMemberDrawDisable(_ model: TFMember)
     {
        self.drawPower = false
        if (model.isMe) {
            self.TF_toast("你的涂鸦权限已被收回")
         
            self.courseLive.drawing.whiteboardTools.isHidden = true
        }
    }
 //   #pragma mark  奖励
    func rtcAward(_ model: TalkfunRtcModel)
    {
        if (self.talkfunSDK.pageMode == TalkfunPageModeCourse) {
            self.courseLive.videoArea.updateModel(model)
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeKeyNote) {
            if (model.xid  == self.zhujiangxid ) {
                self.keynoteLive.keynoteView.updetaModel(model, lianmai:true)
            } else {
                self.keynoteLive.videoArea.update1to1Model(model)
            }
        } else if (self.talkfunSDK.pageMode == TalkfunPageModeGallery){
            self.galleryLive.videoArea.updateModel(model)
        }
        
        let chatData = TF1to16LiveChatData()
        chatData.nickname = model.nickname
        chatData.score = model.score
        chatData.isAward = true
        //奖励
        self.chatArea.setAwardModel(chatData)
        if (model.isMe) {
        self.superView.addSubview(self.awardView)
        self.awardView.rotateView()

        }

    }
    deinit {
            print("纯小班没有强引用===")
        }
}
